import {Service, action} from "adajs";
import List from "../../lists";
import CourseapList from "../../lists/courseap";
import User from "./../../../lib/user";
import {getFirstDate, getLastDate} from "./../../../lib/helper";

class NewsService extends Service {
    defaultData() {
        if (User.isAdmin()) {
            return {
                id: "",
                rows: [
                    [{name: "三月", active: false, value: 3}, {name: "四月", active: false, value: 4}, {
                        name: "五月",
                        active: false,
                        value: 5
                    }, {
                        name: "六月",
                        active: false,
                        value: 6
                    }],
                    [{name: "九月", active: false, value: 9}, {name: "十月", active: false, value: 10}, {
                        name: "十一月",
                        active: false,
                        value: 11
                    }, {
                        name: "十二月",
                        active: false,
                        value: 12
                    }]
                ],
                tabs: [
                    {
                        name: "全部课程",
                        active: true,
                        type: List,
                        option: {
                            url: "/api/lgb/app/lgb/courseinfor/queryPage",
                            type: CourseapList,
                            parameter: {}
                        }
                    }
                ]
            };
        } else {
            return {
                id: "",
                rows: [
                    [{name: "三月", active: false, value: 3}, {name: "四月", active: false, value: 4}, {
                        name: "五月",
                        active: false,
                        value: 5
                    }, {
                        name: "六月",
                        active: false,
                        value: 6
                    }],
                    [{name: "九月", active: false, value: 9}, {name: "十月", active: false, value: 10}, {
                        name: "十一月",
                        active: false,
                        value: 11
                    }, {
                        name: "十二月",
                        active: false,
                        value: 12
                    }]
                ],
                tabs: [
                    {
                        name: "全部课程",
                        active: true,
                        type: List,
                        option: {
                            url: "/api/lgb/app/lgb/courseinfor/queryPage",
                            type: CourseapList,
                            parameter: {
                                kssj: "",
                                jssj: ""
                            }
                        }
                    },
                    {
                        name: "我的课程",
                        active: false,
                        type: List,
                        option: {
                            url: "/api/lgb/app/lgb/courseinfor/queryMyselfCourse",
                            type: CourseapList,
                            parameter: {
                                username: User.getUserInfo().userName
                            }
                        }
                    }
                ]
            };
        }
    }

    onupdate(current, id) {
        if (id) {
            current.id = id;
        }
    }

    @action("active")
    active(current, cell) {
        current.rows.forEach(row => {
            row.forEach(_cell => {
                if (_cell.name === cell.name) {
                    _cell.active = true;
                } else {
                    _cell.active = false;
                }
            });
        });
        current.tabs.forEach(tab => {
            let year = new Date().getFullYear();
            tab.option.parameter.kssj = getFirstDate(year, cell.value).replace(/\//g, "-");
            tab.option.parameter.jssj = getLastDate(year, cell.value).replace(/\//g, "-");
        });
    }
}

export default NewsService;